package pl.edu.pjwstk.kor.sbql4j.project2;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Customer;
import pl.edu.pjwstk.kor.model.Deal;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project2Query_SbqlQuery3Db4o0 implements Db4oSBQLQuery {
    private java.lang.Double dealCost;

    public Project2Query_SbqlQuery3Db4o0(java.lang.Double dealCost) {
        this.dealCost = dealCost;
    }

    /**
     * query='db.(Customer as c where  sum c.getDeals().getCost() < dealCost)'
    '
     **/
    public java.util.Collection<pl.edu.pjwstk.kor.model.Customer> executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Customer> _ident_Customer =
            new java.util.ArrayList<pl.edu.pjwstk.kor.model.Customer>();
        ClassMetadata _classMeta3 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Customer");
        long[] _ids3 = _classMeta3.getIDs(transLocal);

        for (long _id3 : _ids3) {
            LazyObjectReference _ref3 = transLocal.lazyReferenceFor((int) _id3);
            _ident_Customer.add((pl.edu.pjwstk.kor.model.Customer) _ref3.getObject());
        }

        java.util.Collection<pl.edu.pjwstk.kor.model.Customer> _asResult_c = _ident_Customer;
        java.util.Collection<pl.edu.pjwstk.kor.model.Customer> _whereResult = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Customer>();
        int _whereLoopIndex = 0;

        for (pl.edu.pjwstk.kor.model.Customer _whereEl : _asResult_c) {
            if (_whereEl == null) {
                continue;
            }

            if (_whereEl != null) {
                ocb.activate(_whereEl, 1);
            }

            pl.edu.pjwstk.kor.model.Customer _ident_c = _whereEl;

            if (_ident_c != null) {
                ocb.activate(_ident_c, 1);
            }

            pl.edu.pjwstk.kor.model.Customer _dotEl = _ident_c;

            if (_ident_c != null) {
                ocb.activate(_ident_c, 2);
            }

            java.util.List<pl.edu.pjwstk.kor.model.Deal> _mth_getDealsResult = _dotEl.getDeals();

            if (_mth_getDealsResult != null) {
                ocb.activate(_mth_getDealsResult, 2);
            }

            java.util.List<java.lang.Double> _dotResult1 = new java.util.ArrayList<java.lang.Double>();
            int _dotIndex1 = 0;

            for (pl.edu.pjwstk.kor.model.Deal _dotEl1 : _mth_getDealsResult) {
                if (_dotEl1 == null) {
                    continue;
                }

                if (_dotEl1 != null) {
                    ocb.activate(_dotEl1, 1);
                }

                java.lang.Double _mth_getCostResult = _dotEl1.getCost();

                if (_mth_getCostResult != null) {
                    ocb.activate(_mth_getCostResult, 1);
                }

                if (_mth_getCostResult != null) {
                    ocb.activate(_mth_getCostResult, 1);
                }

                _dotResult1.add(_mth_getCostResult);
                _dotIndex1++;
            }

            Number _sum0 = null;

            for (Number _sumEl0 : _dotResult1) {
                _sum0 = MathUtils.sum(_sum0, _sumEl0);
            }

            java.lang.Double _sumResult = (java.lang.Double) _sum0;
            java.lang.Double _ident_dealCost = dealCost;

            Boolean _lessResult = _sumResult < _ident_dealCost;

            if (_lessResult) {
                _whereResult.add(_whereEl);
            }

            _whereLoopIndex++;
        }

        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_whereResult, ocb);

        return _whereResult;
    }
}
